﻿//创建时间: 2011-04-20
//创建作者: 李 城
//功能说明: 约克CMS版本声明       
//最后修改: 2011-04-20
/**********************************************************************************************************************************************************
 * 约克CMS版本声明               结点限制       PSN限制      发布信息条数限制    商业版权标识   内容模型                             编号
 * 试用版: TrialVersion            5              1              1000                 √            3/不可编辑/不可添加                0
 * 标准版: StandardVersion         60             1              20000                ×            3/可编辑/不可添加                  1
 * 企业版: EnterpriseVersion      200             3               *                   ×            *                                  2
 * 门户版: PortalsVersion           *             *               *                   ×            *                                  3
***********************************************************************************************************************************************************/
//#define IsDemo   //demo版本
//#define TrialVersion       //试用版
//#define StandardVersion    //标准版
//#define EnterpriseVersion  //企业版
//#define PortalsVersion     //门户版
//#define DevelopVersion     //内部开发版本
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml.Serialization;
using TRPOP.Common;

namespace TRPOP.CMS.Plugin
{
    /// <summary>
    /// 版本信息类
    /// </summary>
    public class VersionLimit
    {
        #region 基本属性
        /// <summary>
        /// 当前 版本信息
        /// </summary>
        public static readonly string VersionInfo;
        /// <summary>
        /// 当前 版本限制提示信息
        /// </summary>
        public static readonly string VersionErrorInfo;
        /// <summary>
        /// 发布版本
        /// </summary>
        public static readonly string VersionLevel = "TRPOP_CMS V" + "2.0.0";//+ " Released ";
        /// <summary>
        /// 当前版本限制的结点数目
        /// </summary>
        public static readonly int NodeLimit;
        /// <summary>
        /// 当前版本PSN限制
        /// </summary>
        public static readonly int PSNLimit;
        /// <summary>
        /// 当前版本信息数量限制
        /// </summary>
        public static readonly int ContentLimit;
        /// <summary>
        /// 是否是试用版
        /// </summary>
        public static readonly bool IsTrial;
        /// <summary>
        /// 试用版提示信息
        /// </summary>
        public static readonly string TrialHtmlPowerBy;
        /// <summary>
        /// 内容模型限制
        /// </summary>
        public static readonly int ContentModelLimit;
        /// <summary>
        /// 内容模型 编辑权限
        /// </summary>
        public static readonly bool ContentModelEdit;
        /// <summary>
        /// 内容模型 添加权限
        /// </summary>
        public static readonly bool ContentModelAdd;
        /// <summary>
        /// 是否是Demo版
        /// </summary>
        public static readonly bool IsDemo = false;
        /// <summary>
        /// 当前版权信息
        /// </summary>
        public static AuthorizationInfo mAuthorizationInfo = GetAuthorization();
        /// <summary>
        /// 当前使用者名称
        /// </summary>
        public static readonly string strEnterpriseName;
        /// <summary>
        /// 当前使用者授权时间
        /// </summary>
        public static readonly DateTime dtEnterpriseStartTime;
        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        static VersionLimit()
        {
            /*
             *   如果是内部开发版本，则直接编译如下的代码 
             * 
             */
#if DevelopVersion
            strEnterpriseName = "河南约克集团";
            dtEnterpriseStartTime = DateTime.Parse("3/4/2011 00:00:00");
            VersionInfo = "内部开发版本";
            NodeLimit = -1;
            PSNLimit = -1;
            ContentLimit = -1;
            ContentModelLimit = -1;
            ContentModelEdit = true;
            ContentModelAdd = true;
            IsTrial = false;
            return;
#endif
            /*
             * 如果当前检测不到license 信息， 并且版本号 <==0 and >3 ，那么不在授权范围内，所以只提供试用版的服务
             */
            if (mAuthorizationInfo == null || mAuthorizationInfo.SoftVersion <= 0 || mAuthorizationInfo.SoftVersion > 3)
            {
                dtEnterpriseStartTime = DateTime.Now;
                strEnterpriseName = "试用版";
                VersionInfo = "试用版";
                NodeLimit = 5;
                PSNLimit = 1;
                ContentLimit = 1000;
                ContentModelLimit = 3;
                ContentModelEdit = false;
                ContentModelAdd = false;
                IsTrial = true;
                TrialHtmlPowerBy = string.Format(
                                                @"<div  style='width:100%; background:transparent; text-align:center'>Powered by 
                                                   <strong><a href='{0}' target='_blank'>{1}</a></strong>
                                              </div>",
                                                "http://The Relentless Pursuit Of Perfection",
                                                VersionLevel
                                                );
            }
            else
            {
                /*
                 *   如果符合版权信息，那么获取企业名称、企业授权时间，企业版本号码
                 *                         并设置相关的版本限制
                 * 
                 */
                strEnterpriseName = mAuthorizationInfo.EnterpriseName;
                dtEnterpriseStartTime = mAuthorizationInfo.EnterpriseLiceTime;
                switch (mAuthorizationInfo.SoftVersion)
                {
                    case 1:
                        VersionInfo = "标准版";
                        NodeLimit = 60;
                        PSNLimit = 1;
                        ContentLimit = 20000;
                        ContentModelLimit = 3;
                        ContentModelEdit = true;
                        ContentModelAdd = false;
                        IsTrial = false;
                        break;
                    case 2:
                        VersionInfo = "企业版";
                        NodeLimit = 200;
                        PSNLimit = 3;
                        ContentLimit = -1;
                        ContentModelLimit = -1;
                        ContentModelEdit = true;
                        ContentModelAdd = false;
                        IsTrial = false;
                        break;
                    case 3:
                        VersionInfo = "门户版";
                        NodeLimit = -1;
                        PSNLimit = -1;
                        ContentLimit = -1;
                        ContentModelLimit = -1;
                        ContentModelEdit = true;
                        ContentModelAdd = true;
                        IsTrial = false;
                        break;
                    default:
                        break;
                }
            }
            #region 之前的动态编译的代码
            /*                                  之前的动态编译的代码
#if TrialVersion//试用版
            VersionInfo = "试用版";
            NodeLimit = 5;
            PSNLimit = 1;
            ContentLimit = 1000;
            ContentModelLimit = 3;
            ContentModelEdit = false;
            ContentModelAdd = false;
            IsTrial = true;
            TrialHtmlPowerBy = string.Format(
                                            @"<div  style='width:100%; background:transparent; text-align:center'>Powered by 
                                                   <strong><a href='{0}' target='_blank'>{1}</a></strong>
                                              </div>",
                                            "http://www.yorkbbs.ca",
                                            VersionLevel
                                            );
#elif StandardVersion//标准版
            VersionInfo = "标准版";
            NodeLimit = 60;
            PSNLimit = 1;
            ContentLimit = 20000;
            ContentModelLimit = 3;
            ContentModelEdit = true;
            ContentModelAdd = false;
            IsTrial = false;
#elif EnterpriseVersion//企业版
            VersionInfo = "企业版";
            NodeLimit = 200;
            PSNLimit = 3;
            ContentLimit = -1;
            ContentModelLimit = -1;
            ContentModelEdit = true;
            ContentModelAdd = false;
            IsTrial = false;
#elif PortalsVersion//门户版
            VersionInfo = "门户版";
            NodeLimit = -1;
            PSNLimit = -1;
            ContentLimit = -1;
            ContentModelLimit = -1;
            ContentModelEdit = true;
            ContentModelAdd = true;
            IsTrial = false;
#elif DevelopVersion//内部开发版本
            VersionInfo = "内部开发版本";
            NodeLimit = -1;
            PSNLimit = -1;
            ContentLimit = -1;
            ContentModelLimit = -1;
            ContentModelEdit = true;
            ContentModelAdd = true;
            IsTrial = false;
#endif
*/
            #endregion
            VersionErrorInfo = string.Format("{0}不支持当前操作,请升级版本", VersionInfo);
        }

        /// <summary>
        /// 获取限制的数量
        /// </summary>
        /// <param name="intSelect"></param>
        /// <param name="intLimit"></param>
        /// <returns></returns>
        public static int GetLimitNums(int intSelect, int intLimit)
        {
            if (intLimit == -1)
                return intSelect;
            if (intSelect == 0)
                return intLimit;
            return intLimit > intSelect ? intSelect : intLimit;
        }
        /// <summary>
        /// 获取当前CMS程序使用企业的版权信息
        /// </summary>
        /// <returns></returns>
        private static AuthorizationInfo GetAuthorization()
        {
            return getEnterpriseInfo(TRPOP.Common.FileUtil.ReadFile(Common.Util.GetMapPath("/license")));
            /*
            *
            原来的测试代码
            AuthorizationInfo oAuthorizationInfo = new AuthorizationInfo { EnterpriseLiceNum = "123456", EnterpriseLiceTime = DateTime.Now, EnterpriseName = "河南思迪有限公司", SoftVersion = 1 };
            return oAuthorizationInfo;
             *
             * 
             */
        }
        /// <summary>
        ///  获取用户信息
        /// </summary>
        /// <param name="strSource"></param>
        /// <returns></returns>
        public static AuthorizationInfo getEnterpriseInfo(string strSource)
        {
            AuthorizationInfo oAuthorizationInfo = new AuthorizationInfo();
            if (strSource.IsEmpty())
                return oAuthorizationInfo;
            strSource = StringExtension.DESDecode(strSource, "TRPOP_Workshop".MD5Hash());
            strSource = strSource.Remove(0, 8);
            strSource = strSource.Remove(strSource.Length - 5);
            try
            {
                using (StringReader sr = new StringReader(strSource))
                {
                    XmlSerializer serializer = new XmlSerializer(oAuthorizationInfo.GetType());
                    oAuthorizationInfo = serializer.Deserialize(sr) as AuthorizationInfo;
                }
            }
            catch { }
            return oAuthorizationInfo;
        }
    }
}
